/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package game.Sprites;

import game.Helper;
import game.Scene;
import game.SceneObject;
import game.Vector3;
import javafx.scene.canvas.GraphicsContext;

import javafx.scene.paint.Color;
import java.awt.geom.Rectangle2D;
import java.util.Comparator;

public abstract class Sprite extends SceneObject {

    protected Vector3 position;
    protected double rotation;

    protected Color color; //Para test con formas geometricas basicas


    /**
     * @param position The position of the sprite in the world
     * @param rotation The rotation of the sprite (using radians)
     */
    public Sprite(Vector3 position, double rotation, Color color) throws Exception {
        if(Scene.getInstance() == null){
            throw new Exception("Sprite cannot be created without a scene.");
        }
        Scene.getInstance().registerSprite(this);

        this.position = new Vector3(position);
        this.rotation = rotation;
        this.color = color;
    }

    @Override
    public void destroy() {
        super.destroy();
        Scene.getInstance().deleteSprite(this);
    }

    /**
     * @param x        The x coordinate of the sprite in the world
     * @param y        The y coordinate of the sprite in the world
     * @param z        The z coordinate of the sprite in the world
     * @param rotation The rotation of the sprite (using radians)
     */
    public Sprite(double x, double y, double z, double rotation) throws Exception{
        this.position = new Vector3(x, y, z);
        this.rotation = rotation;
    }

    /**
     * @return the position of the sprite in the world
     */
    public Vector3 getPosition() {
        return new Vector3(this.position);
    }

    /**
     * @return the rotation (radians)
     */
    public double getRotation() {
        return this.rotation;
    }

    /**
     * @return the x
     */
    public double getX() {
        return this.position.x;
    }

    /**
     * @return the y
     */
    public double getY() {
        return this.position.y;
    }

    /**
     * @return the z
     */
    public double getZ() {
        return this.position.z;
    }


    /**
     * @param x the x to set
     */
    public void setX(double x) {
        this.position.x = x;
    }

    /**
     * @param y the y to set
     */
    public void setY(double y) {
        this.position.y = y;
    }

    /**
     * @param z the z to set
     */
    public void setZ(double z) {
        this.position.z = z;
    }

    /**
     * @param position the position to set
     */
    public void setPosition(Vector3 position) {
        this.position = new Vector3(position);
    }


    /**
     * @param rotation the rotation to set in radians
     */
    public void setRotation(double rotation) {
        this.rotation = rotation;
    }

    public abstract void draw(GraphicsContext gd, Vector3 screenPos, double squareRes);

    public abstract Rectangle2D.Double getBound();

    public Color getColor() {
        return color;
    }

    public void setColor(Color color) {
        this.color = color;
    }

    public static class SortByDepth implements Comparator<Sprite>
    {
        // Used for sorting in ascending order of z coord
        public int compare(Sprite a, Sprite b) {
            return (int)a.getZ() - (int)b.getZ();
        }
    }

}
